import 'dart:async';
import 'dart:io';
import 'package:acaleph/model/base_model.dart';
import 'package:acaleph/model/test_detail_model.dart';
import 'package:acaleph/model/test_list_model.dart';
import 'package:acaleph/mvp/repository/list_repository.dart';
import 'package:acaleph/mvp/repository/test_detail_repository.dart';
import 'package:acaleph/utils/http_util.dart';
import 'package:dio/dio.dart';

class ListRepositoryImpl implements ListRepository {
  @override
  Future<BaseModel<List<TestListModel>>> loadList(int length) {
    return _getThemes(length);
  }
}

Future<BaseModel<List<TestListModel>>> _getThemes(int length) async {
  int code;

  String errorMsg;

  BaseModel<List<TestListModel>> model;

  List<TestListModel> themesList;

  int arg1 = ((length/10)+1).toInt();

  try {
    Response response = await new HttpUtil().post("https://dsn.apizza.net/mock/d9ece8d859722233416fa78ba498d717/newsList", data: {
      'pageNum': arg1,
      'pageSize': 10,
      'type': 2,
      'sortColumn': 'add_time'
    });
   print(response.data['data'].toString());
    code = response.statusCode;


    if (response.statusCode == HttpStatus.OK) {
      List themes = response.data['data']['list'];

      themesList = themes.map((model) {
        return new TestListModel.fromJson(model);
      }).toList();
    } else {
      errorMsg = '服务器异常';
    }
  } catch (exception) {
    errorMsg = '您的网络似乎出了什么问题';
  } finally {
    model = new BaseModel(code: code, errorMsg: errorMsg, data: themesList);
  }

  return model;
}
